# delimit ;
clear ;
cd "replication" ;
set more off ; 
 
* *************************************************************************** ;
* load attendance data 
* *************************************************************************** ;

use "./data/attendance.dta", clear ;

****************************************************************************** ;
* generate randomization strata and wave-class variable
****************************************************************************** ;

egen strata = group(wave neighborhood female mfi) ;
egen waveclass = group(wave class) ;

* *************************************************************************** ;
* label variables for the table ; 
* *************************************************************************** ;

label var healthonly "HEE" ;
label var healthandpay "HEEC" ; 

forvalues i = 1/5 { ;
	label var attended`i' "\shortstack[l]{Attended\\session `i'}" ;
} ;

label var attended_atleast1 "\shortstack[l]{Attended\\at least\\one session}" ; 
label var attended_all "\shortstack[l]{Attended\\all five\\sessions}" ;
label var totattended "\shortstack[l]{Total number\\ of sessions\\attended}" ; 

****************************************************************************** ;
* merge with baseline data to get baseline health index
****************************************************************************** ;

* merge with baseline data ;
merge 1:1 Id using "./data/baseline" ; 

* Baseline Health Index ; 
gen bl_health_index = (aids_curable + nightblindness_curable + wash_defecation + water_boil_filter + water_tap)/5 ; 
summarize bl_health_index ;

* ***************************************************************************** ;
* regression 1: effects on attendance
* ***************************************************************************** ;

est drop _all ;

foreach var of varlist attended1-attended5 attended_atleast1 attended_all totattended { ;
	
	areg `var' healthonly healthandpay, a(strata) robust ;
	qui est sto `var' ;
	
	test healthonly healthandpay ; 
	estadd local titlerow1 = "" ; 
	estadd scalar fstat1 = r(F): `var' ; 
	estadd scalar pval1 = r(p): `var' ; 

	test healthonly = healthandpay ; 
	estadd local titlerow2 = "" ; 
	estadd scalar fstat2 = r(F): `var' ; 
	estadd scalar pval2 = r(p): `var' ; 

	summarize `var' if control == 1 & e(sample) == 1;
	estadd scalar cmean = r(mean): `var' ; 
	estadd scalar csd = r(sd): `var' ;

} ;

* Output, Panel A ;
esttab attended1 attended2 attended3 attended4 attended5 attended_atleast1 attended_all totattended using "./output/appendix-table-attendance-rates-panelA.tex",
	replace
	drop(_cons) 
	cells(b(label() star fmt(%9.3f %9.3f)) se(par)) 
	star(* 0.10 ** 0.05 *** 0.01) 
	stats(titlerow fstat1 pval1 titerow2 fstat2 pval2 cmean csd N, fmt(%9s %9.3f %9.3f %9s %9.3f %9.3f %9.3f %9.3f %9.0f) 
	labels("\(F\)-test, HEE = 0 \& HEEC = 0" "$\qquad$ \(F\)-statistic" "$\qquad$ \(p\)-value" 
	"\(F\)-test, HEE = HEEC" "$\qquad$ \(F\)-statistic" "$\qquad$ \(p\)-value"
	"Control Mean" "Control SD" "N"))
	prehead(\begin{table}[htbp] \centering \small `"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"'
		\captionsetup{justification=centering} 
		\caption{Program Attendance Rates}
		\label{appendix-table-attendance-rates}
		\setlength{\tabcolsep}{10.5pt}
		\begin{tabular*}{\hsize}{p{4.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}}
		\toprule)
	legend label  booktabs  collabels( , none)
	mgroups(
	"By Session"
	"Overall Attendance Measures"
	, pattern(1 0 0 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	posthead(\midrule \end{tabular*}
	\medskip
	\captionsetup[sub]{justification=raggedleft} 
	\subcaption*{Panel A: Main Effects} 
	\vspace{-0.5ex}
	\newcolumntype{C}{>{\centering\arraybackslash}X} 
	\setlength{\tabcolsep}{6pt}
	\begin{tabularx}{\hsize}{lCCCCCCCC}
	\midrule
	&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}{c}{(3)}
	&\multicolumn{1}{c}{(4)} &\multicolumn{1}{c}{(5)} &\multicolumn{1}{c}{(6)} 
	&\multicolumn{1}{c}{(7)} &\multicolumn{1}{c}{(8)}
	\\ \midrule)
	postfoot(`"\midrule"'  \end{tabularx}) ;

* ***************************************************************************** ;
* regression 2: effects on attendance, heterogeneity by baseline health index
* ***************************************************************************** ;

* generate interactions with baseline health index ; 
gen healthonlyXbl_health_index = healthonly*bl_health_index ; 
gen healthandpayXbl_health_index = healthandpay*bl_health_index ; 

* label variables for the table ; 
label var healthonlyXbl_health_index "HEE $\ast$ Baseline Health Index" ;
label var healthandpayXbl_health_index "HEEC $\ast$ Baseline Health Index" ;

est drop _all ;

foreach var of varlist attended1-attended5 attended_atleast1 attended_all totattended { ;
	
	areg `var' healthonly healthandpay bl_health_index 
	healthonlyXbl_health_index healthandpayXbl_health_index, a(strata) robust ;
	qui est sto `var' ;

} ;

* Output, Panel B ;
esttab attended1 attended2 attended3 attended4 attended5 attended_atleast1 attended_all totattended using "./output/appendix-table-attendance-rates-panelB.tex",
	replace nodepvars nomtitles
	drop(_cons bl_health_index)
	eqlabels(none)
	cells(b(label() star fmt(%9.3f %9.3f)) se(par)) 
	star(* 0.10 ** 0.05 *** 0.01) 
	stats( N, fmt(%9.0f) labels("N"))
	prehead(\medskip
	\subcaption*{\small Panel B: Heterogeneity by Baseline Health Index}
	\vspace{-0.5ex}
	\setlength{\tabcolsep}{6pt}
	\begin{tabularx}{\hsize}{lCCCCCCCC}
	\midrule)
	label booktabs collabels( , none)
	postfoot(`"\midrule"'  \end{tabularx}
	\captionsetup{justification=justified, width=\linewidth} 
	\caption*{\footnotesize \textit{Notes:} 
		All outcome variables are dummies except for the last column, which represents the total number of sessions 
		attended (integer from 0 to 5). 
		\textit{HEE} is a dummy for assignment to only health entertainment-education. 
		\textit{HEEC} is a dummy for assignment to health entertainment-education with cash incentives for test performance. 
	\textit{Baseline Health Index} is the proportion of all five baseline health variables (reported in Table \ref{table-baseline-summstats}) that are equal to one for the respondent.
		Robust standard errors. 
		All regressions include \textit{Baseline Health Index} as a separate variable and dummies for randomization strata, where strata are defined by gender, neighborhood, and microfinance client status.
		***\$\,p < 0.01$, **\$\,p < 0.05$, *\$\,p<0.10$.
		}
	\end{table}) ;

exit ;

